/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TransaksiPenjualan.java
 *
 * Created on Aug 11, 2010, 11:28:41 PM
 */
package com.ivanbiz.gui;

import com.ivanbiz.dao.BarangDAO;
import com.ivanbiz.dao.NotaDAO;
import com.ivanbiz.dao.TukarBarangDAO;
import com.ivanbiz.helper.CenterRenderer;
import com.ivanbiz.helper.RightRenderer;
import com.ivanbiz.helper.ServiceHelper;
import com.ivanbiz.impl.BarangDAOImpl;
import com.ivanbiz.impl.NotaDAOImpl;
import com.ivanbiz.impl.TukarBarangDAOImpl;
import com.ivanbiz.model.Barang;
import com.ivanbiz.model.ItemNota;
import com.ivanbiz.model.Nota;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

/**
 *
 * @author Aprisma
 */
public class TukarPenjualan extends javax.swing.JInternalFrame {

    ServiceHelper serviceHelper = new ServiceHelper();
    NotaDAO notaDAO = new NotaDAOImpl();
    Nota nota = null;
    BarangDAO barangDAO = new BarangDAOImpl();
    TukarBarangDAO tukarBarangDAO = new TukarBarangDAOImpl();
    List<ItemNota> listItemNota = new ArrayList<ItemNota>();
    List<Barang> listBarangTukar = new ArrayList<Barang>();
    List<Barang> listBarangTambah = new ArrayList<Barang>();
    List<Barang> listBarang = new ArrayList<Barang>();
    NumberFormat numberFormat = NumberFormat.getInstance(Locale.US);
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd MMMM yyyy");
    double totals = 0;

    /** Creates new form TransaksiPenjualan */
    public TukarPenjualan() {
        initComponents();
        updateData();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        groupPilihan = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        labelTanggal = new javax.swing.JLabel();
        textTanggal = new javax.swing.JTextField();
        labelKode = new javax.swing.JLabel();
        textKode = new javax.swing.JTextField();
        labelCustomer = new javax.swing.JLabel();
        textPelanggan = new javax.swing.JTextField();
        labelCustomer1 = new javax.swing.JLabel();
        textStatusPembayaran = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        buttonProses = new javax.swing.JButton();
        buttonReset = new javax.swing.JButton();
        textTotal = new javax.swing.JTextField();
        labelPT = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableItemNota = new javax.swing.JTable();
        textCari = new javax.swing.JTextField();
        comboBarang = new javax.swing.JComboBox();
        buttonTukar = new javax.swing.JButton();
        buttonTambah = new javax.swing.JButton();
        labelKode1 = new javax.swing.JLabel();
        comboCari = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();

        setBorder(new org.jdesktop.swingx.border.DropShadowBorder());
        setClosable(true);
        setIconifiable(true);
        setMaximizable(true);
        setTitle("Tukar / Tambah Penjualan");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Penjualan"));

        labelTanggal.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelTanggal.setText("Tanggal");

        textTanggal.setEditable(false);
        textTanggal.setFont(new java.awt.Font("Tahoma", 1, 11));
        textTanggal.setText("jTextField1");

        labelKode.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelKode.setText("Kode");

        textKode.setFont(new java.awt.Font("Tahoma", 1, 11));
        textKode.setText("jTextField2");
        textKode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                textKodeKeyPressed(evt);
            }
        });

        labelCustomer.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelCustomer.setText("Pelanggan");

        textPelanggan.setEditable(false);
        textPelanggan.setFont(new java.awt.Font("Tahoma", 1, 11));
        textPelanggan.setText("jTextField1");

        labelCustomer1.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelCustomer1.setText("Status Pembayaran");

        textStatusPembayaran.setEditable(false);
        textStatusPembayaran.setFont(new java.awt.Font("Tahoma", 1, 11));
        textStatusPembayaran.setText("jTextField1");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(labelTanggal)
                    .addComponent(labelKode)
                    .addComponent(labelCustomer)
                    .addComponent(labelCustomer1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textStatusPembayaran, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                    .addComponent(textKode, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                    .addComponent(textTanggal, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE)
                    .addComponent(textPelanggan, javax.swing.GroupLayout.DEFAULT_SIZE, 172, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelTanggal)
                    .addComponent(textTanggal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textKode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelKode))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(labelCustomer)
                    .addComponent(textPelanggan, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textStatusPembayaran, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelCustomer1))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Total"));

        buttonProses.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonProses.setText("Prosess");
        buttonProses.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonProsesActionPerformed(evt);
            }
        });

        buttonReset.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonReset.setText("Reset");
        buttonReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonResetActionPerformed(evt);
            }
        });

        textTotal.setFont(new java.awt.Font("Tahoma", 1, 11));
        textTotal.setText("jTextField1");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(textTotal, javax.swing.GroupLayout.DEFAULT_SIZE, 564, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonReset, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(buttonProses)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(buttonProses)
                    .addComponent(buttonReset)
                    .addComponent(textTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        labelPT.setFont(new java.awt.Font("Tahoma", 1, 36));
        labelPT.setText("PT BERGUNA JAYA");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Tabel Penjualan"));

        tableItemNota.setAutoCreateRowSorter(true);
        tableItemNota.setFont(new java.awt.Font("Tahoma", 1, 11));
        tableItemNota.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(tableItemNota);

        textCari.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        textCari.setText("jTextField1");
        textCari.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                textCariKeyReleased(evt);
            }
        });

        comboBarang.setFont(new java.awt.Font("Tahoma", 1, 11));
        comboBarang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        buttonTukar.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonTukar.setText("Tukar");
        buttonTukar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTukarActionPerformed(evt);
            }
        });

        buttonTambah.setFont(new java.awt.Font("Tahoma", 1, 11));
        buttonTambah.setText("Tambah");
        buttonTambah.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                buttonTambahActionPerformed(evt);
            }
        });

        labelKode1.setFont(new java.awt.Font("Tahoma", 1, 11));
        labelKode1.setText("Barang");

        comboCari.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        comboCari.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 742, Short.MAX_VALUE)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(labelKode1)
                        .addGap(29, 29, 29)
                        .addComponent(comboBarang, 0, 513, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonTambah)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(buttonTukar))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel3Layout.createSequentialGroup()
                        .addComponent(comboCari, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(textCari, javax.swing.GroupLayout.DEFAULT_SIZE, 586, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(textCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(comboCari, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboBarang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(buttonTukar)
                    .addComponent(labelKode1)
                    .addComponent(buttonTambah))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                .addContainerGap())
        );

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel4.setText("Jln. Jembatan Selatan no 470 Panglima Polim Jakarta Selatan ");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14));
        jLabel5.setText("Telp : 08174820800");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelPT)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                            .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(labelPT)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabel5))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-800)/2, (screenSize.height-550)/2, 800, 550);
    }// </editor-fold>//GEN-END:initComponents

    private void buttonProsesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonProsesActionPerformed
        try {
            nota.setItemNota(listItemNota);
            tukarBarangDAO.tukarBarang(nota, listBarangTukar, listBarangTambah);
            updateData();
        } catch (Exception ex) {
            Logger.getLogger(TukarPenjualan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_buttonProsesActionPerformed

    private void textKodeKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textKodeKeyPressed
        if (evt.getKeyCode() == 10) {
            try {
                textKode.setEditable(false);
                buttonProses.setEnabled(true);
                buttonTambah.setEnabled(true);
                buttonTukar.setEnabled(true);
                nota = (Nota) notaDAO.getDataByEqual(Nota.class, "kode", textKode.getText().toUpperCase());
                textPelanggan.setText(nota.getCustomer().getKode() + "( " + nota.getCustomer().getNama() + " )");
                textStatusPembayaran.setText(nota.getStatusPembayaran());
                listItemNota = nota.getItemNota();
                updateTableItemNota();
                updateTotal();
            } catch (Exception ex) {
                Logger.getLogger(TukarPenjualan.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_textKodeKeyPressed

    private void buttonResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonResetActionPerformed
        if (nota != null) {
            nota = null;
        }
        updateData();
    }//GEN-LAST:event_buttonResetActionPerformed

    private void buttonTukarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTukarActionPerformed
        try {
            textKode.setEditable(false);
            float jumlah = Float.parseFloat(JOptionPane.showInputDialog(null, "Input Jumlah Barang Yang Ditukar", ""));
            ItemNota itemNota = listItemNota.get(tableItemNota.getSelectedRow());
            int row = tableItemNota.getSelectedRow();
            if (jumlah > itemNota.getJumlah()) {
                JOptionPane.showMessageDialog(null, "Jumlah Melebihi batas Tukar !!!");
            } else if (jumlah <= 0.0) {
                JOptionPane.showMessageDialog(null, "Jumlah Tidak Boleh Kurang atau sama dengan 0 !!!");
            } else {
                Barang barang = (Barang) barangDAO.getDataByEqual(Barang.class, "id", itemNota.getBarang().getId());
                barang.setKode(barang.getKode());
                barang.setNama(barang.getNama());
                barang.setStock(jumlah);
                listBarangTukar.add(barang);
                itemNota.setJumlah(itemNota.getJumlah() - jumlah);
                itemNota.setCreateDate(ServiceHelper.getDateNow());
                listItemNota.set(row, itemNota);
                updateTableItemNota();
                updateTotal();
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Jumlah Harus Angka !!!");
            Logger.getLogger(TukarPenjualan.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_buttonTukarActionPerformed

    private void buttonTambahActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonTambahActionPerformed
        try {
            textKode.setEditable(false);
            double harga = Double.parseDouble(JOptionPane.showInputDialog(null, "Input Harga Jual", ""));
            float jumlah = Float.parseFloat(JOptionPane.showInputDialog(null, "Input Jumlah Barang", ""));
            Barang barang = (Barang) listBarang.get(comboBarang.getSelectedIndex());
            if (jumlah <= 0.0) {
                JOptionPane.showMessageDialog(null, "Jumlah Tidak Boleh Kurang atau sama dengan 0 !!!");
            } else if (barang.getStock() <= 0.0 || barang.getStock() < jumlah) {
                JOptionPane.showMessageDialog(null, "Stock Tidak Cukup !!!");
            } else {
                barang.setKode(barang.getKode());
                barang.setNama(barang.getNama());
                barang.setStock(jumlah);
                barang.setHarga(harga);
                listBarangTambah.add(barang);
                ItemNota itemNota = new ItemNota();
                itemNota.setBarang(barang);
                itemNota.setNota(nota);
                itemNota.setHargaJual(harga);
                itemNota.setHargaModal(barang.getHarga());
                itemNota.setCreateDate(ServiceHelper.getDateNow());
                itemNota.setJumlah(jumlah);
                listItemNota.add(itemNota);
                updateTableItemNota();
                updateTotal();
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, "Harga atau Jumlah Harus Angka !!!");
            Logger.getLogger(TukarPenjualan.class.getName()).log(Level.SEVERE, null, ex);
        }
}//GEN-LAST:event_buttonTambahActionPerformed

    private void textCariKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_textCariKeyReleased
        try {
            listBarang = barangDAO.getDataByLike(Barang.class, comboCari.getSelectedItem().toString(), textCari.getText());
            updateComboBarang();
        } catch (Exception ex) {
            Logger.getLogger(TransaksiPenjualan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_textCariKeyReleased

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton buttonProses;
    private javax.swing.JButton buttonReset;
    private javax.swing.JButton buttonTambah;
    private javax.swing.JButton buttonTukar;
    private javax.swing.JComboBox comboBarang;
    private javax.swing.JComboBox comboCari;
    private javax.swing.ButtonGroup groupPilihan;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labelCustomer;
    private javax.swing.JLabel labelCustomer1;
    private javax.swing.JLabel labelKode;
    private javax.swing.JLabel labelKode1;
    private javax.swing.JLabel labelPT;
    private javax.swing.JLabel labelTanggal;
    private javax.swing.JTable tableItemNota;
    private javax.swing.JTextField textCari;
    private javax.swing.JTextField textKode;
    private javax.swing.JTextField textPelanggan;
    private javax.swing.JTextField textStatusPembayaran;
    private javax.swing.JTextField textTanggal;
    private javax.swing.JTextField textTotal;
    // End of variables declaration//GEN-END:variables

    private void updateComboBarang() {
        Object data[] = new Object[listBarang.size()];
        int x = 0;
        for (Barang barang : listBarang) {
            data[x] = barang.getKode() + " (" + barang.getNama() + " Rp " + numberFormat.format(barang.getHarga()) + ",00" + ")" + " stock " + barang.getStock() + " / " + barang.getSatuan().getNama();
            x++;
        }
        comboBarang.setModel(new DefaultComboBoxModel(data));
    }

    private void updateComboBarangByFind(String nama) {
        Iterator iter = this.listBarang.iterator();
        while (iter.hasNext()) {
            Barang barang = (Barang) iter.next();
            if (!barang.getNama().startsWith(nama)) {
                iter.remove();
            }
        }
        Object data[] = new Object[listBarang.size()];
        int x = 0;
        for (Barang barang : listBarang) {
            data[x] = barang.getKode() + " (" + barang.getNama() + " Rp " + numberFormat.format(barang.getHarga()) + ",00" + ")" + " stock " + barang.getStock() + " / " + barang.getSatuan().getNama();
            x++;
        }
        comboBarang.setModel(new DefaultComboBoxModel(data));
    }

    private void updateComboCari() {
        Object[] dataClass = serviceHelper.getPropertyClass(Barang.class);
        Object[] dataSearch = new Object[3];
        dataSearch[0] = dataClass[1];
        dataSearch[1] = dataClass[2];
        dataSearch[2] = dataClass[6];
        comboCari.setModel(new DefaultComboBoxModel(dataSearch));
    }

    private void updateData() {
        try {
            textKode.setEditable(true);
            buttonProses.setEnabled(false);
            buttonTambah.setEnabled(false);
            buttonTukar.setEnabled(false);
            textCari.setText("");
            textPelanggan.setText("");
            textStatusPembayaran.setText("");
            textTanggal.setText(simpleDateFormat.format(new Date()));
            textKode.setText("");
            textKode.setEditable(true);
            textTotal.setText("");
            listItemNota.clear();
            updateTableItemNota();
            listBarangTukar.clear();
            listBarangTambah.clear();
            listBarang = barangDAO.getAll(Barang.class);
            updateComboBarang();
            updateComboCari();
        } catch (Exception ex) {
            Logger.getLogger(TukarPenjualan.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void updateTableItemNota() {
        String[] judul = {"No", "Kode", "Nama", "Satuan", "Jumlah", "Harga", "Sub Total"};
        Object[][] isi = new Object[listItemNota.size()][7];
        int x = 0;
        int no = 0;
        for (ItemNota itemNota : listItemNota) {
            isi[x][0] = no += 1;
            isi[x][1] = itemNota.getBarang().getKode();
            isi[x][2] = itemNota.getBarang().getNama();
            isi[x][3] = itemNota.getBarang().getNama();
            isi[x][4] = itemNota.getJumlah();
            isi[x][5] = "Rp " + numberFormat.format(itemNota.getHargaJual()) + ",00";
            isi[x][6] = "Rp " + numberFormat.format(itemNota.getHargaJual() * itemNota.getJumlah()) + ",00";
            x++;

        }
        tableItemNota.setModel(new DefaultTableModel(isi, judul) {

            @Override
            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return false;
            }
        });
        TableColumn column0 = tableItemNota.getColumnModel().getColumn(0);
        column0.setPreferredWidth(75);
        column0.setMaxWidth(75);
        column0.setCellRenderer(new CenterRenderer());
        TableColumn column1 = tableItemNota.getColumnModel().getColumn(1);
//        column1.setPreferredWidth(100);
//        column1.setMaxWidth(100);
        column1.setCellRenderer(new CenterRenderer());
        TableColumn column3 = tableItemNota.getColumnModel().getColumn(3);
//        column3.setPreferredWidth(75);
//        column3.setMaxWidth(75);
        column3.setCellRenderer(new CenterRenderer());
        TableColumn column4 = tableItemNota.getColumnModel().getColumn(4);
//        column4.setPreferredWidth(140);
//        column4.setMaxWidth(140);
        column4.setCellRenderer(new CenterRenderer());
        TableColumn column5 = tableItemNota.getColumnModel().getColumn(5);
//        column5.setPreferredWidth(140);
//        column5.setMaxWidth(140);
        column5.setCellRenderer(new RightRenderer());
        TableColumn column6 = tableItemNota.getColumnModel().getColumn(6);
//        column5.setPreferredWidth(140);
//        column5.setMaxWidth(140);
        column6.setCellRenderer(new RightRenderer());
    }

    private void updateTotal() {
        double total = 0;
        for (ItemNota itemNota : listItemNota) {
            total += itemNota.getHargaJual() * itemNota.getJumlah();
        }
        totals = total;
        textTotal.setText("Rp " + numberFormat.format(total) + ",00");
    }
}
